//
//  LoginViewController.m
//  MingJiangApp
//
//  Created by wangliang on 15/3/9.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "LoginViewController.h"
#import "BCBaseObject.h"
#import "AppDelegate.h"

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = Color_Back_Black;
    self.nameTextField.keyboardType = UIKeyboardTypeNumberPad;
   
}
- (IBAction)close:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
- (IBAction)longin:(id)sender {
    
    [self.nameTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
    
    if (![BCBaseObject isMobileNumber:self.nameTextField.text]) {
        
        [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"手机号码不正确"];
    }else if (self.passwordTextField.text.length == 0) {
        
        [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"密码不能为空"];
    }else {
        
        NSDictionary *dic = @{@"user_name":self.nameTextField.text,
                              @"password":self.passwordTextField.text};
        [WLHttpRequestAfter userLoginWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
            
            if ([resultDictionary[@"status"] integerValue] == 100) {
                
                [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"用户不存在"];
            }else if ([resultDictionary[@"status"] integerValue] == 2) {
                
                [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"密码错误"];
            }else if ([resultDictionary[@"status"] integerValue] == 1) {
                
                [userInfoTool updateInfo:resultDictionary[@"data"]];
//                AppDelegate * appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//                [appdelegate choseRootVc];
                [self dismissViewControllerAnimated:YES completion:nil];
                [[NSNotificationCenter defaultCenter] postNotificationName:K_NOTICE_MINE object:nil];
            }else{
            
                [[DMCAlertCenter defaultCenter] postAlertWithMessage:resultDictionary[@"msg"]];
            }
        } andFailureBlock:^(NSError *resultError) {
            
        }];

    }
        
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)back:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
